<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<style>
    body,ul,li,p{
        padding:0;
        margin:0;
    }
    body{
        background-color:#ccc;
    }
    ul{
        width: 500px;
        margin:100px auto;
    }
    li{
        width: 400px;
        height: 50px;
        padding:0 50px;
        list-style:none;
        background-color:lightblue;
        font:16px/50px microsoft yahei;
    }
    input{
        width: 50px;
        height: 30px;
        margin:0 5px;
    }
    p{
        width: 500px;
        height: 50px;
        margin:0 auto;
        text-align:center;
        font:16px/50px microsoft yahei;
        background-color:pink;
    }
</style>
<body>
<ul>
    <li><input type="button" value="-" class="subtract"><span class="number">0</span><input type="button" value="+" class="plus">单价：<span class="perprice">12.5</span>元 小计：<span class="subtotal">0</span>元</li>
    <li><input type="button" value="-" class="subtract"><span class="number">0</span><input type="button" value="+" class="plus">单价：<span class="perprice">10.5</span>元 小计：<span class="subtotal">0</span>元</li>
    <li><input type="button" value="-" class="subtract"><span class="number">0</span><input type="button" value="+" class="plus">单价：<span class="perprice">8.5</span>元 小计：<span class="subtotal">0</span>元</li>
    <li><input type="button" value="-" class="subtract"><span class="number">0</span><input type="button" value="+" class="plus">单价：<span class="perprice">8</span>元 小计：<span class="subtotal">0</span>元</li>
    <li><input type="button" value="-" class="subtract"><span class="number">0</span><input type="button" value="+" class="plus">单价：<span class="perprice">4.51</span>元 小计：<span class="subtotal">0</span>元</li>
</ul>
<p>商品合计：<span class="piece">0</span>件，共花了：<span class="totalprice">0</span>元</p>
<p>所购商品中最贵的单价是：<span class="expensive">0</span>元</p>
</body>
<script>
    var Subtract=document.getElementsByClassName("subtract");
    var Plus=document.getElementsByClassName("plus");
    var Num=document.getElementsByClassName("number");
    var Subtotal=document.getElementsByClassName("subtotal");
    var Perprice=document.getElementsByClassName("perprice");
    var Piece=document.getElementsByClassName("piece")[0];
    var Totalprice=document.getElementsByClassName("totalprice")[0];
    var Expensive=document.getElementsByClassName("expensive")[0];
    var arr=[0,0,0,0,0];
    var cnt=0;
    SUB(Subtract);
    PLUS( Plus);

    //减法运算
    function SUB(sub){
        for(var i=0;i<sub.length;i++){
            sub[i].index=i;
            sub[i].onclick=function(){
                if(Num[this.index].innerHTML>0) {
                    Num[this.index].innerHTML--;//单个商品数量
                    Subtotal[this.index].innerHTML=(Perprice[this.index].innerHTML*Num[this.index].innerHTML).toFixed(2);//小计
                    Piece.innerHTML=--cnt;//商品总个数
                    (Totalprice.innerHTML-=Perprice[this.index].innerHTML).toFixed(2);//总价
                    if(Num[this.index].innerHTML===0) arr[this.index]=0;//选取所购商品单价最高
                    Expensive.innerHTML=getMax(arr);
                }
            }
        }
    }

    //加法运算
    function PLUS(plus){
        for(var i=0;i<plus.length;i++){
            plus[i].index=i;
            plus[i].onclick=function(){
                Num[this.index].innerHTML++;//单个商品数量
                Subtotal[this.index].innerHTML=(Perprice[this.index].innerHTML*Num[this.index].innerHTML).toFixed(2);//小计
                Piece.innerHTML=++cnt;//商品总个数
                Totalprice.innerHTML=((Number(Totalprice.innerHTML)+Number(Perprice[this.index].innerHTML))).toFixed(2);//总价
                if(Num[this.index].innerHTML!==0) arr[this.index]=Number(Perprice[this.index].innerHTML);//选取所购商品单价最高
                Expensive.innerHTML=getMax(arr);
            }

        }
    }

    //对所购商品单价排序
    function getMax(array){
        var max=array[0];
        for(var k=1;k<array.length;k++){
            if(array[k]>max){
                max=array[k];
            }
        }
        return max;
    }
</script>
</html>